package cn.wolfcode.service.impl;

import cn.wolfcode.common.exception.BusinessException;
import cn.wolfcode.domain.AccountTransaction;
import cn.wolfcode.domain.OperateIntergralVo;
import cn.wolfcode.mapper.AccountTransactionMapper;
import cn.wolfcode.mapper.UsableIntegralMapper;
import cn.wolfcode.service.IUsableIntegralService;
import cn.wolfcode.web.msg.IntergralCodeMsg;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import io.seata.rm.tcc.api.BusinessActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * Created by lanxw
 */
@Service
public class UsableIntegralServiceImpl implements IUsableIntegralService {
    @Autowired
    private UsableIntegralMapper usableIntegralMapper;
    @Autowired
    private AccountTransactionMapper accountTransactionMapper;

    @Override
    public void decrIntergral(OperateIntergralVo vo) {
        int count = usableIntegralMapper.decrIntergral(vo.getUserId(),vo.getValue());
        if (count == 0){
            //操作积分失败
            throw new BusinessException(IntergralCodeMsg.OP_INTERGRAL_ERROR);
        }
    }

    @Override
    public void incrIntergral(OperateIntergralVo vo) {
        usableIntegralMapper.incrIntergral(vo.getUserId(),vo.getValue());
    }

    @Override
    @Transactional
    public void incrIntergralTry(OperateIntergralVo operateIntergralVo, BusinessActionContext context) {
        AccountTransaction accountTransaction = new AccountTransaction();
        accountTransaction.setTxId(context.getXid()); //全局事务id
        accountTransaction.setActionId(context.getBranchId()); //分支事务id
        accountTransaction.setAmount(operateIntergralVo.getValue()); //变动金额
        accountTransaction.setUserId(operateIntergralVo.getUserId()); //用户id
        Date now = new Date();
        accountTransaction.setGmtCreated(now); //创建时间
        accountTransaction.setGmtModified(now); //修改时间
        accountTransactionMapper.insert(accountTransaction);
        //做try业务逻辑，退款是空操作
    }

    @Override
    @Transactional
    public void incrIntergralConfirm(BusinessActionContext context) {
        AccountTransaction transaction = accountTransactionMapper.get(context.getXid(), context.getBranchId());
        if (transaction != null) {
            //有记录
            if (AccountTransaction.STATE_TRY == transaction.getState()){
                //如果属于try,执行confirm逻辑
                //把事务状态修改为Confirm状态
                accountTransactionMapper.updateAccountTransactionState(context.getXid(),context.getBranchId(),AccountTransaction.STATE_COMMIT,AccountTransaction.STATE_TRY);
                //confirm方法逻辑，给对应的账户加积分
                JSONObject jsonObject  = (JSONObject) context.getActionContext("operateIntergralVo");
                OperateIntergralVo vo = jsonObject.toJavaObject(OperateIntergralVo.class);
                usableIntegralMapper.incrIntergral(vo.getUserId(),vo.getValue());
            }else {
                //之前执行过confirm ， 不做事情
            }
        }else {
            //没有记录
        }

    }

    @Override
    @Transactional
    public void incrIntergralCancel(BusinessActionContext context) {
        AccountTransaction accountTransaction = accountTransactionMapper.get(context.getXid(), context.getBranchId());
        JSONObject jsonObject  = (JSONObject) context.getActionContext("operateIntergralVo");
        OperateIntergralVo vo = jsonObject.toJavaObject(OperateIntergralVo.class);
        if (accountTransaction != null){
            if (AccountTransaction.STATE_TRY == accountTransaction.getState()){
                //说明第一次进来，把事务状态修改成为Concel
                accountTransactionMapper.updateAccountTransactionState(context.getXid(),context.getBranchId(),AccountTransaction.STATE_CANCEL,AccountTransaction.STATE_TRY);
                //执行Cancel逻辑
                //空操作
            }else {
                //处理幂等性问题
            }
        }else {
            //如果事务记录为空，此时空回调，防悬挂
        }
    }
}
